package com.jt.www.biz.order_pc.controller;

import com.jt.www.biz.order_pc.service.TaxpayerInfoService;
import com.jt.www.model.insured.TaxpayerInfoEntity;
import com.jt.www.model.reps.GenericDataResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "开票信息接口", tags = {"TAXPAYER-INFO-API"}, description = "开票信息维护接口")
@RestController
@RequestMapping(value = "/taxpayer")
public class TaxpayerInfoController {

    public static final Logger log = LoggerFactory.getLogger(TaxpayerInfoController.class);

    @Autowired
    private TaxpayerInfoService taxpayerInfoService;

    /**
     * 修改开票信息
     *
     * @param taxpayerInfoEntity
     * @return
     */
    @ApiOperation(value = "修改开票信息", notes = "修改开票信息", httpMethod = "POST")
    @PostMapping(value = "/update")
    public ResponseEntity updateInsuredUserInfo(@RequestBody TaxpayerInfoEntity taxpayerInfoEntity) {

        Integer num = taxpayerInfoService.updateTaxpayerInfo(taxpayerInfoEntity);
        if (num == 1) {
            return GenericDataResponse.ok();
        } else if (num == -1) {
            return GenericDataResponse.errorWithMsg("参数不能为空");
        } else if (num == 0) {
            return GenericDataResponse.ok();
        }
        return GenericDataResponse.errorWithMsg("修改失败");
    }

    /**
     * 删除开票信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除开票信息", notes = "删除开票信息", httpMethod = "GET")
    @GetMapping(value = "/delete")
    public ResponseEntity deleteTaxpayerInfo(@ApiParam(value = "开票信息id", required = true) @RequestParam("id") String id) {

        if (StringUtils.isEmpty(id)) {
            log.error("【删除开票信息】id为空");
            return GenericDataResponse.errorWithMsg("参数不能为空");
        }
        Integer num = taxpayerInfoService.deleteTaxpayerInfo(id);
        if (num == 1) {
            return GenericDataResponse.ok();
        }
        return GenericDataResponse.errorWithMsg("删除失败");
    }

    /**
     * 根据usercode查询开票信息
     *
     * @param usercode
     * @return
     */
    @ApiOperation(value = "根据usercode查询开票信息", notes = "根据usercode查询开票信息", httpMethod = "GET")
    @GetMapping(value = "/getTaxpayerInfoByUsercode")
    public ResponseEntity searchTaxpayerInfoByUsercode(@ApiParam(value = "唯一标识 投保人userCode;被保险人id;", required = true) @RequestParam("usercode") String usercode,
                                                       @ApiParam(value = "类型：1-投保人；2-被保险人；", required = true) @RequestParam("type") String type) {

        if (StringUtils.isEmpty(usercode)) {
            log.error("【根据usercode查询开票信息】usercode为空");
            return GenericDataResponse.errorWithMsg("参数不能为空");
        }

        List<TaxpayerInfoEntity> taxpayerInfoEntities = taxpayerInfoService.searchTaxpayerInfoByUserCode(usercode, type);
        return GenericDataResponse.okWithData(taxpayerInfoEntities);
    }
}
